/*
 *  Copyright (C) 2013  Venkat Raghavan G
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  
 */

package edu.uic.cs.analyze.commons;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

// TODO: Auto-generated Javadoc
/**
 * The Class FileUtils.
 */
public class FileUtils {
	

	/**
	 * Gets the file content as set.
	 *
	 * @param fileName the file name
	 * @param ignoreCase the ignore case
	 * @return the file content as set
	 */
	public static Set<String> getFileContentAsSet(File fileName,
			boolean ignoreCase) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Set<String> fileContent = new HashSet<String>();
		String strLine;
		try {
			while ((strLine = br.readLine()) != null) {
				strLine = strLine.trim();
				if (ignoreCase)
					fileContent.add(strLine.toLowerCase());
				else
					fileContent.add(strLine);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return fileContent;
	}

	/**
	 * Gets the file content as map.
	 *
	 * @param fileName the file name
	 * @param delimiterRegex the delimiter regex
	 * @param ignoreCase the ignore case
	 * @return the file content as map
	 */
	public static HashMap<String, String> getFileContentAsMap(File fileName,
			String delimiterRegex, boolean ignoreCase) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HashMap<String, String> fileContent = new HashMap<String, String>();
		;

		String strLine;
		try {
			while ((strLine = br.readLine()) != null) {
				strLine = strLine.trim();
				String strArray[] = strLine.split(delimiterRegex);
				String srcString = "";
				String destString = "";
				if (strArray.length == 2)
					destString = strArray[1];
				else if (strArray.length != 1)
					continue;
				srcString = strArray[0];
				if (ignoreCase)
					srcString = srcString.toLowerCase();
				fileContent.put(srcString, destString);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return fileContent;
	}

	/**
	 * Gets the string integer map.
	 *
	 * @param fileContent the file content
	 * @return the string integer map
	 */
	public static HashMap<String, Integer> getStringIntegerMap(
			HashMap<String, String> fileContent) {
		HashMap<String, Integer> temp = new HashMap<String, Integer>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(key, Integer.parseInt(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the string double map.
	 *
	 * @param fileContent the file content
	 * @return the string double map
	 */
	public static HashMap<String, Double> getStringDoubleMap(
			HashMap<String, String> fileContent) {
		HashMap<String, Double> temp = new HashMap<String, Double>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(key, Double.parseDouble(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the integer string map.
	 *
	 * @param fileContent the file content
	 * @return the integer string map
	 */
	public static HashMap<Integer, String> getIntegerStringMap(
			HashMap<String, String> fileContent) {
		HashMap<Integer, String> temp = new HashMap<Integer, String>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Integer.parseInt(key), fileContent.get(key));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the integer integer map.
	 *
	 * @param fileContent the file content
	 * @return the integer integer map
	 */
	public static HashMap<Integer, Integer> getIntegerIntegerMap(
			HashMap<String, String> fileContent) {
		HashMap<Integer, Integer> temp = new HashMap<Integer, Integer>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Integer.parseInt(key),
						Integer.parseInt(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the integer double map.
	 *
	 * @param fileContent the file content
	 * @return the integer double map
	 */
	public static HashMap<Integer, Double> getIntegerDoubleMap(
			HashMap<String, String> fileContent) {
		HashMap<Integer, Double> temp = new HashMap<Integer, Double>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Integer.parseInt(key),
						Double.parseDouble(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the double string map.
	 *
	 * @param fileContent the file content
	 * @return the double string map
	 */
	public static HashMap<Double, String> getDoubleStringMap(
			HashMap<String, String> fileContent) {
		HashMap<Double, String> temp = new HashMap<Double, String>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Double.parseDouble(key), fileContent.get(key));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the double integer map.
	 *
	 * @param fileContent the file content
	 * @return the double integer map
	 */
	public static HashMap<Double, Integer> getDoubleIntegerMap(
			HashMap<String, String> fileContent) {
		HashMap<Double, Integer> temp = new HashMap<Double, Integer>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Double.parseDouble(key),
						Integer.parseInt(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * Gets the double double map.
	 *
	 * @param fileContent the file content
	 * @return the double double map
	 */
	public static HashMap<Double, Double> getDoubleDoubleMap(
			HashMap<String, String> fileContent) {
		HashMap<Double, Double> temp = new HashMap<Double, Double>();
		for (String key : fileContent.keySet()) {
			try {
				temp.put(Double.parseDouble(key),
						Double.parseDouble(fileContent.get(key)));
			} catch (Exception e) {
				System.out.println("Warning: Incompatible data type detected.");
			}
		}
		return temp;
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String, String> x = FileUtils.getFileContentAsMap(new File(
				"resources/TokenMap"), "\t", false);

		System.out.println(getStringIntegerMap(x));
	}

}
